﻿using BaseUtils.Helper;
using BaseUtils.Utils;
using DatabaseCore.Sqlite;
using Service.src.Flow.Server;
using Service.src.NW.Model;
using Service.src.NW.Model.UserPageProject;
using System.IO;
using System.Runtime.InteropServices;

namespace Service.src.Flow.Api
{

    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class UserPageProjectApi
    {

        public static UserPageProjectServer ProjectServer;

        /// <summary>
        /// 查找项目
        /// </summary>
        /// <returns></returns>
        public UserPageProjectApi()
        {
            ProjectServer = new UserPageProjectServer();
        }

        /// <summary>
        /// 查找项目
        /// </summary>
        /// <returns></returns>
        public string SearchProject()
        {
            return ProjectServer.SearchProject(SqliteHelper.SystemDefaultDBID).ToJson();
        }

        public string GetProjectInfoById(int Id)
        {
            string Path = $"{AppData.RuntimeFlowDataViewDirectory + Id}//{AppData.UserPage}default.{AppData.UserPageComponent}";
            string ComponentJson = FileHelper.ReadContent(Path);
            return ComponentJson;
        }

        /// <summary>
        /// 新增项目
        /// </summary>
        /// <param name="ProjectJson"></param>
        /// <returns></returns>
        //public string AddProject()
        //{
        //    UserPageProject project = new UserPageProject();
        //    project.Title = "新项目";
        //    return ProjectServer.AddProject(SqliteHelper.SystemDefaultDBID,project).ToJson();
        //}

        /// <summary>
        /// 更新项目
        /// </summary>
        /// <param name="ProjectJson"></param>
        /// <returns></returns>
        //public int UpdateProject(string ProjectJson)
        //{
        //    UserPageProject project = ConvertHelper.ToObject<UserPageProject>(ProjectJson);
        //    return ProjectServer.UpdateProject(SqliteHelper.SystemDefaultDBID, project);
        //}

        /// <summary>
        /// 根据ID删除项目
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool DeleteProject(int Id)
        {
            return ProjectServer.DeleteProject(SqliteHelper.SystemDefaultDBID, Id);
        }

        /// <summary>
        /// 发布项目
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public int ReleasedProjectById(int Id)
        {
            return ProjectServer.ReleasedProject(SqliteHelper.SystemDefaultDBID, Id);
        }

        //public string CopyUserProject(int ProjectId)
        //{
        //    UserPageProject p = ProjectServer.CopyUserProject(SqliteHelper.SystemDefaultDBID, ProjectId);
        //    string sourceFolderPath = $"{AppData.RuntimeFlowDataViewDirectory + ProjectId}";
        //    string destinationFolderPath = $"{AppData.RuntimeFlowDataViewDirectory + p.Id}";
        //    CopyAllFilesInFolder(sourceFolderPath, destinationFolderPath);
        //    return p.ToJson();
        //}

        private void CopyAllFilesInFolder(string sourceFolderPath, string destinationFolderPath)
        {
            // 创建目标文件夹（如果不存在）
            Directory.CreateDirectory(destinationFolderPath);

            // 获取源文件夹下的所有文件
            string[] files = Directory.GetFiles(sourceFolderPath);

            foreach (string file in files)
            {
                // 获取文件名（包含扩展名）
                string fileName = Path.GetFileName(file);
                // 组合目标文件路径
                string destinationFilePath = Path.Combine(destinationFolderPath, fileName);
                // 复制文件
                File.Copy(file, destinationFilePath, true);
            }
        }
    }
}
